//货物管理

interface IGoodsManagentDetailViewModel {
    /**
     * 编号
     */
    id?: string;
    /**
     * 货物名称
     */
    name: string;
    /**
     * 产品代号
     */
    productCode: string;
    /**
     * 货物类别编号
     */
    categoryId: string;
    /**
     * 辅助工具
     */
    auxiliaryTool: string;
    /**
     * 包装方式
     */
    packageMethod: string;
    /**
     * 毛重换算率
     */
    gwRate: number;
    /**
     * 货物尺寸-长度
     */
    length: number;
    /**
     * 货物尺寸-宽度
     */
    width: number;
    /**
     * 货物尺寸-高度
     */
    height: number;
    /**
     * 货物容量
     */
    volume: string;
    /**
     * 货物重量
     */
    weight: number;
    /**
     * 货物重量单位
     */
    weightUnit: string;
    /**
     * 货物规格
     */
    goodNorms: string;
    /**
     * 赔偿方式
     */
    compensation: string;
    /**
     * 备注
     */
    remarks: string;
    /**
     * 点击事件
     */
    click: () => void;

}

interface IGoodsManagentViewModel extends IBaseViewModel<IGoodsManagentQueryTerms> {
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<GoodsListItemResponse>;
    /**
     * 货物类别下拉
     */
    categoryTypeDropDown: Array<GoodsTypeListItemResponse>;
    /**
     * 货物重量单位下拉
     */
    weightUnitDropDown: ValueListItemResponse[];
    /**
     * 添加/编辑详情
     */
    detail: IGoodsManagentDetailViewModel;
    /**
     * 添加货物
     */
    addGoods: () => void;
    /**
     * 编辑货物
     */
    editGoods: (id: string) => void;
    /**
     * 删除货物
     */
    deleteGoods: (id: string) => void;
    /**
     * 查询
     */
    query:()=>void;
    /**
     * 客户单位名称
     */
    clientName:string;
    /**
     * 列表高
     */
    heightA:number;
}

interface IGoodsManagentQueryTerms {
    /**
     * 货物名称
     */
    name: string;
    /**
     * 货物分类
     */
    categoryId: string;
}

class goodsManagentController extends ListController<IGoodsManagentViewModel, IGoodsManagentQueryTerms>{
    gridApi: uiGrid.IGridApiOf<GoodsListItemResponse>;

    enterpriseId: string;

    constructor($scope: IGoodsManagentViewModel, private goodsService: IGoodsService, private valueService: IValueService,
        private goodsTypeService: IGoodsTypeService, private $ngBootbox: BootboxService, private $location: ng.ILocationService) {
        super($scope);
        this.init();
    }

    init(): void {
        super.init();
        this.enterpriseId = JSON.parse(window.localStorage.getItem("client").toString()).clientId;
        this.$scope.clientName = JSON.parse(window.localStorage.getItem("client").toString()).clientName;

        this.$scope.addGoods = this.showAddGoodsDialog;
        this.$scope.editGoods = this.showEditGoodsDialog;
        this.$scope.deleteGoods = this.showDeleteGoodsDialog;

        this.$scope.queryTerms = {
            name: "",
            categoryId: ""
        };

        this.$scope.gridOptions = {
            enableSorting: false,
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            paginationPageSizes: [10, 20, 30],
            columnDefs: [
                { name: "序号", field: "index", enableColumnMenu: false },
                { name: "产品代号", field: "productCode", enableColumnMenu: false },
                { name: "产品名称", field: "name", enableColumnMenu: false },
                { name: "包装方式", field: "packageMethod", enableColumnMenu: false },
                { name: "货物类别", field: "categoryName", enableColumnMenu: false },
                {
                    name: "操作",
                    cellFilter: 'checkmark',
                    cellTemplate: '<a data-toggle="modal" title="编辑" data-target="#goods" class="fa fa-edit m-l-xs primary text-info" ng-click="grid.appScope.editGoods(row.entity.id)"></a> <a class="primary fa fa-trash-o text-danger" title="删除" ng-click="grid.appScope.deleteGoods(row.entity.id)"></a>',
                    enableColumnMenu: false
                }
            ],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
            }
        };
        this.query();
        this.$scope.query = this.query;
        this.loadCategoryTypeDropDown();
        this.loadWeightUnitDropDown();

    }

    loadData(queryTerms: IGoodsManagentQueryTerms, skip: number, count: number): void {
        this.goodsService.getList(this.enterpriseId, queryTerms.name, queryTerms.categoryId, skip, count).then(result => {
            this.$scope.gridOptions.totalItems = result.data.total;
            result.data.data.map((item, index) => item.index = skip++ +1);
            this.$scope.gridOptions.data = result.data.data;

            this.$scope.heightA = (this.$scope.gridOptions.data.length * 39 ) +95;

            if (skip == 0) {
                this.$scope.gridOptions.paginationCurrentPage = 1;
            }
            this.$scope.querying = false;
        }, error => {
            this.$scope.querying = false;
        });
    }

    /**
     * 货物类别下拉
     */
    loadCategoryTypeDropDown(): void {
        this.goodsTypeService.getList(this.enterpriseId, "", 0, -1).then(result => {
            this.$scope.categoryTypeDropDown = result.data.data;
        });
    }

    /**
     * 读取货物重量单位下拉数据
     */
    loadWeightUnitDropDown(): void {
        this.$scope.weightUnitDropDown = this.valueService.getGoodQuantityUnitList().data;
    }

    showDeleteGoodsDialog = (id: string) => {
        this.$ngBootbox.confirm("确定删除此数据吗？").then(e => {
            this.goodsService.delete(id).then(result => {
                this.reload();
            });
        });
    }

    showAddGoodsDialog = () => {
        this.$scope.title = "添加货物";
        this.$scope.detail = {
            name: "",
            productCode: "",
            categoryId: "",
            auxiliaryTool: "",
            packageMethod: "",
            gwRate: null,
            length: null,
            width: null,
            height: null,
            volume: "",
            weight: null,
            weightUnit: "",
            goodNorms: "",
            compensation: "",
            remarks: "",
            click: this.addGoods
        };
    }

    addGoods=()=> {
        if (!$("#goodsForm").valid()) { return }
        this.$ngBootbox.confirm("确定保存此数据吗？").then(e => {
            this.goodsService.add(
                this.enterpriseId,
                this.$scope.detail.name,
                this.$scope.detail.productCode,
                this.$scope.detail.categoryId,
                this.$scope.detail.auxiliaryTool,
                this.$scope.detail.packageMethod,
                this.$scope.detail.gwRate,
                this.$scope.detail.length,
                this.$scope.detail.width,
                this.$scope.detail.height,
                this.$scope.detail.volume,
                this.$scope.detail.weight,
                this.$scope.detail.weightUnit,
                this.$scope.detail.goodNorms,
                this.$scope.detail.compensation,
                this.$scope.detail.remarks
            ).then(result => {
                this.reload();
                $("#goods").modal("hide");
            });
        });
    }

    showEditGoodsDialog = (id: string) => {
        this.$scope.title = "编辑货物";
        var validator = $('#goodsForm').validate();
        validator.form();
        validator.hideErrors();
        $(".error").removeClass("error");
        this.goodsService.getDetail(id).then(result => {
            this.$scope.detail = {
                id: id,
                name: result.data.name,
                productCode: result.data.productCode,
                categoryId: result.data.categoryId,
                auxiliaryTool: result.data.auxiliaryTool,
                packageMethod: result.data.packageMethod,
                gwRate: result.data.gwRate,
                length: result.data.length,
                width: result.data.width,
                height: result.data.height,
                volume: result.data.volume,
                weight: result.data.weight,
                weightUnit: result.data.weightUnit,
                goodNorms: result.data.goodNorms,
                compensation: result.data.compensation,
                remarks: result.data.remarks,
                click: this.editGoods
            };
        });
    }

    editGoods=()=> {
        this.$ngBootbox.confirm("确定保存该数据吗？").then(e => {
            this.goodsService.edit(
                this.$scope.detail.id,
                this.enterpriseId,
                this.$scope.detail.name,
                this.$scope.detail.productCode,
                this.$scope.detail.categoryId,
                this.$scope.detail.auxiliaryTool,
                this.$scope.detail.packageMethod,
                this.$scope.detail.gwRate,
                this.$scope.detail.length,
                this.$scope.detail.width,
                this.$scope.detail.height,
                this.$scope.detail.volume,
                this.$scope.detail.weight,
                this.$scope.detail.weightUnit,
                this.$scope.detail.goodNorms,
                this.$scope.detail.compensation,
                this.$scope.detail.remarks
            ).then(result => {
                this.reload();
                $("#goods").modal("hide");
            });
        });
    }
}

angular.module("tms").controller("goodsManagentController", ["$scope", "goodsService", "valueService", "goodsTypeService", "$ngBootbox", "$location",goodsManagentController]);